package com.itheima.health.controller;


import com.itheima.health.resource.code.ValidateCodeUtils;
import com.itheima.health.resource.entity.Result;
import com.itheima.health.resource.pub.MessageConstant;
import com.itheima.health.resource.pub.RedisMessageConstant;
import com.itheima.health.service.MemberService;
import com.itheima.health.service.OrderService;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 *   微信端的后台 业务处理
 */
@RestController
@RequestMapping("order")
public class OrderController {


    @Reference
    private OrderService orderService;

    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping("add.do")
    public  Result  add(@RequestBody Map map){
        try {
             //  第一步：  判断验证码 是否有效
            String  validateCode = (String)map.get("validateCode");
            if(StringUtils.isNotBlank(validateCode)){
                String  telephone = (String)map.get("telephone");
                String key =RedisMessageConstant.SENDTYPE_ORDER+telephone; //  获取redis  指定的预约验证码
                String redisCode = (String)redisTemplate.opsForValue().get(key);
              if(StringUtils.isBlank(redisCode)){
                  return  new Result(false,"验证码过期");
              }else if(!validateCode.equals(redisCode)){
                  return  new Result(false,"验证码错误");
              }else {
                  //  第二步： 验证码正确  预约业务  业务层完成
                  Order order =  orderService.add(map);
                  //  发送短信给客户 短信通知     通知客户 预约成功。。。。
                  return  new Result(true, MessageConstant.ADD_ORDER_SUCCESS,order);
              }
            }else{
                return  new Result(false,"数据非法");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, e.getMessage());
        }
    }



    @RequestMapping("findOrderInfoByOrderId.do")
    public  Result  findOrderInfoByOrderId(@RequestParam("orderid")  Integer orderid){
        try {
            Map map = orderService.findOrderInfoByOrderId(orderid);
            return  new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }


}
